package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin/common")
public class CommonController {

    @Resource
    private AliOSSUtils aliOssUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file){
//        String oldFilename = file.getOriginalFilename();
//        String suffix = oldFilename.substring(oldFilename.lastIndexOf("."));
        //String newFileName = UUID.randomUUID() + suffix;
        try {
            String image = aliOssUtil.upload(file);
            return Result.success(image);
        } catch (IOException e) {
            e.getMessage();
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
